package com.woniu.adminservice.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.adminservice.commeon.CommonResult;
import com.woniu.adminservice.entity.Roles;
import com.woniu.adminservice.exception.RolesServiceExceptipn;
import com.woniu.adminservice.service.RolesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/roles")
public class RolesController {
    @Resource
    private RolesService rolesService;


    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:角色列表页面
     * return:
     */
    @RequestMapping("/list")
    @ResponseBody
    public CommonResult roleList(@RequestParam(value = "page",defaultValue = "1",required = false)Integer currentPage,
                                 @RequestParam(value = "limit",defaultValue = "10",required = false)Integer pageSize){
        List<Roles> roles = rolesService.selectAllByPage(currentPage,pageSize);
        PageInfo pageInfo = new PageInfo(roles);
        return CommonResult.success(pageInfo);
    }


    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:新增角色信息
     * return:
     */
    @RequestMapping("/saveRole")
    @ResponseBody
    public CommonResult saveRole(Roles roles,String menuId){
        //判断角色是否重复
        Roles role =  rolesService.findByRoleName(roles.getRoleName());
        if (role!=null){
            return CommonResult.failed("该角色已存在,请勿重复添加!");
        }
        //将权限id字符串拆分并转为集合形式
        List<Integer>  keys = Arrays.stream(menuId.split(",")).map(s -> Integer.parseInt(s)).collect(Collectors.toList());
        try {
            rolesService.saveRole(roles,keys);
            return CommonResult.success("添加角色成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("添加角色失败!");
        }
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description: 根据id删除单条角色信息
     * return:
     */
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public CommonResult deleteById(@PathVariable("id")Integer id){
        try {
            rolesService.deleteById(id);
            return CommonResult.success("删除成功!");
        }catch (RolesServiceExceptipn e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("删除失败!");
        }
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:根据角色id进行批量删除
     * return:
     */
    @RequestMapping("/batchDel/{roleIds}")
    @ResponseBody
    public CommonResult deleteByRoleIds(@PathVariable("roleIds")String roleIds){
        //将角色id字符串分割并存储到list中
        List<Integer> rolesId = Arrays.stream(roleIds.split(",")).map(s->Integer.parseInt(s)).collect(Collectors.toList());
        try {
            rolesService.deleteByIds(rolesId);
            return CommonResult.success("删除成功!");
        }catch (RolesServiceExceptipn e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("删除失败!");
        }
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:编辑角色页面
     * return:
     */
    @RequestMapping("/editRole/{id}")
    public String editRolePage(@PathVariable("id") Integer id, Model model){
        //根据角色id查询相关信息
        Roles roles = rolesService.selectById(id);
        model.addAttribute("roles",roles);
        return "/sys/editRole";
    }

    /**
     * Auther: 杨世民  <br/>
     * Date: :  <br/>
     * Description:更新角色信息 同时更新角色相关权限
     * return:
     */
    @PostMapping("/updateRoles")
    @ResponseBody
    public CommonResult updateRoles(Roles roles,String menuId){
        //判断更新后的角色是否已经存在
        if(rolesService.checkRoleName(roles)){
          return  CommonResult.failed("该角色已存在,请勿重复添加!");
        }
        List<Integer> menuIDs = Arrays.stream(menuId.split(",")).map(s->Integer.parseInt(s)).collect(Collectors.toList());
        try {
            rolesService.updateRoleById(roles,menuIDs);
           return CommonResult.success("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
           return CommonResult.failed("修改失败!");
        }
    }

}